﻿using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Devonline.Http.AntDesign
{
    /// <summary>
    /// ant design data type
    /// </summary>
    public class AntDesignData
    {
        /// <summary>
        /// data list of type
        /// </summary>
        [JsonPropertyName("list")]
        public IEnumerable List { get; set; }
        /// <summary>
        /// current page index
        /// </summary>
        [JsonPropertyName("current")]
        public int? Current { get; set; }
        /// <summary>
        /// page size
        /// </summary>
        [JsonPropertyName("pageSize")]
        public int? PageSize { get; set; }
        /// <summary>
        /// total rows of data
        /// </summary>
        [JsonPropertyName("total")]
        public int? Total { get; set; }

        /// <summary>
        /// 定义与 PagedResult 的强制类型转换
        /// </summary>
        /// <param name="result"></param>
        public static implicit operator PagedResult(AntDesignData result) => new() { PageIndex = result.Current ?? 1, PageSize = result.PageSize ?? 0, Total = result.Total ?? 0, Rows = result.List };

        /// <summary>
        /// 定义与 PagedResult 的强制类型转换
        /// </summary>
        /// <param name="result"></param>
        public static implicit operator AntDesignData(PagedResult result) => new() { List = result.Rows, Current = result.PageIndex, PageSize = result.PageSize, Total = result.Total };
    }

    /// <summary>
    /// ant design data type
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AntDesignData<T>
    {
        /// <summary>
        /// data list of type
        /// </summary>
        [JsonPropertyName("list")]
        public IEnumerable<T> List { get; set; }
        /// <summary>
        /// current page index
        /// </summary>
        [JsonPropertyName("current")]
        public int? Current { get; set; }
        /// <summary>
        /// page size
        /// </summary>
        [JsonPropertyName("pageSize")]
        public int? PageSize { get; set; }
        /// <summary>
        /// total rows of data
        /// </summary>
        [JsonPropertyName("total")]
        public int? Total { get; set; }

        /// <summary>
        /// 定义与 PagedResult 的强制类型转换
        /// </summary>
        /// <param name="result"></param>
        public static implicit operator PagedResult<T>(AntDesignData<T> result) => new() { PageIndex = result.Current ?? 1, PageSize = result.PageSize ?? 0, Total = result.Total ?? 0, Rows = result.List };

        /// <summary>
        /// 定义与 PagedResult 的强制类型转换
        /// </summary>
        /// <param name="result"></param>
        public static implicit operator AntDesignData<T>(PagedResult<T> result) => new() { List = result.Rows, Current = result.PageIndex, PageSize = result.PageSize, Total = result.Total };
    }
}
